home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ForCLI / MCommands_1_0.lha / Src / changefont.c next >
Encoding:
C/C++ Source or Header  |  1994-03-19  |  4.9 KB  |  183 lines

  1. #define __USE_SYSBASE
  2.  
  3. #include <exec/types.h>
  4. #include <exec/execbase.h>
  5. #include <exec/memory.h>
  6. #include <dos/rdargs.h>
  7. #include <dos/dosextens.h>
  8. #include <graphics/text.h>
  9. #include <support/types.h>
  10. #include <support/exec.h>
  11.  
  12. #include <string.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/diskfont.h>
  17. #include <proto/graphics.h>
  18.  
  19. #include "changefont.rev.h"
  20.  
  21. #define DOS_NAME            "dos.library"
  22. #define DOS_VERN             37L
  23. #define GFX_NAME            "graphics.library"
  24. #define GFX_VERN            0L
  25. #define DISKFONT_NAME    "diskfont.library"
  26. #define DISKFONT_VERN    36L
  27.  
  28. #define STD_NAME            "topaz.font"
  29. #define STD_STYLE            FS_NORMAL
  30. #define STD_FLAGS            (FPF_ROMFONT | FPF_DESIGNED)
  31.  
  32. #define REPLACE_FLAGS    (FPF_DISKFONT | FPF_DESIGNED)
  33.  
  34. #define FLAGS_MASK        (FPF_REVPATH | FPF_TALLDOT | FPF_WIDEDOT | FPF_PROPORTIONAL | FPF_DESIGNED)
  35.  
  36. #define TEMPLATE            "NAME/A,SIZE/N/M,QUIET/S"
  37.  
  38. STATIC CONST TEXT VersionString[]=
  39.     VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);
  40.  
  41. #define MSG_REPLACED            "  Replaced\n"
  42. #define MSG_NOT_REPLACED    "  Not Replaced"
  43. #define INFO_TEMPLATE            "%s %d"
  44.  
  45. #define FONT_EXT            ".font"
  46.  
  47. #define OPT_NAME        0
  48. #define OPT_SIZE        1
  49. #define OPT_QUIET        2
  50. #define OPT_COUNT        3
  51.  
  52. #define NAME_SIZE        36        /* Size of name buffer */
  53. #define BASE_SIZE        25        /* Max allowed length of font's name w/o extension */
  54. #define EXT_SIZE        5            /* Size of font extension */
  55.  
  56. STATIC BOOL Match(struct TextFont *srcFont, struct TextFont *destFont);
  57. STATIC VOID CopyAttrs(struct TextFont *srcFont, struct TextFont *destFont);
  58.  
  59. ULONG ChangeFont(VOID)
  60. {
  61.     struct ExecBase *SysBase=*((struct ExecBase **)4);
  62.     struct DosLibrary *DOSBase;
  63.     struct GfxBase *GfxBase;
  64.     struct Library *DiskfontBase;
  65.  
  66.     STATIC CONST LONG Default8=8,Default9=9;
  67.     STATIC CONST LONG * CONST DefaultSizes[]={&Default8,&Default9,NULL};
  68.  
  69.     LONG Opts[OPT_COUNT];
  70.     ULONG RC=RETURN_FAIL,FontError;
  71.     struct TextAttr StdAttr,ReplaceAttr;
  72.     struct TextFont *StdFont,*ReplaceFont;
  73.     CHAR FontName[NAME_SIZE];
  74.     LONG **SizesPtr,*TempPtr;
  75.     struct RDArgs *Args;
  76.     BOOL ErrOccured=FALSE;
  77.  
  78.     unless(DOSBase=(struct DosLibrary *)OpenLibrary(DOS_NAME,DOS_VERN))
  79.     {
  80.         SetResult2(ERROR_INVALID_RESIDENT_LIBRARY);
  81.         goto InvalidDOS;
  82.     }
  83.  
  84.     unless(GfxBase=(struct GfxBase *)OpenLibrary(GFX_NAME,GFX_VERN))
  85.     {
  86.         PrintFault(IoErr(),NULL);                                /* Inform user */
  87.         goto InvalidGfx;
  88.     }
  89.  
  90.     unless(DiskfontBase=OpenLibrary(DISKFONT_NAME,DISKFONT_VERN))
  91.     {
  92.         PrintFault(IoErr(),NULL);                                /* Inform user */
  93.         goto InvalidDiskfont;
  94.     }
  95.  
  96.     clear(&Opts);                                                            /* Clear out args buffer */
  97.     unless(Args=ReadArgs(TEMPLATE,Opts,NULL))
  98.     {
  99.         PrintFault(IoErr(),NULL);                                /* Inform user */
  100.         goto NoArgs;
  101.     }
  102.  
  103.     StdAttr.ta_Name=STD_NAME;
  104.     StdAttr.ta_Flags=STD_FLAGS;
  105.  
  106.     ReplaceAttr.ta_Name=FontName;
  107.     ReplaceAttr.ta_Flags=REPLACE_FLAGS;
  108.  
  109.     ReplaceAttr.ta_Style=StdAttr.ta_Style=STD_STYLE;
  110.  
  111.     SizesPtr=Opts[OPT_SIZE] ? (LONG **)Opts[OPT_SIZE] : DefaultSizes;
  112.     while(TempPtr=*SizesPtr++)
  113.     {
  114.         FontError=ERROR_OBJECT_NOT_FOUND;    /* Possible error: fon not found */
  115.         ReplaceAttr.ta_YSize=StdAttr.ta_YSize=*TempPtr;
  116.         unless(StdFont=OpenDiskFont(&StdAttr))
  117.             goto NoStdFont;
  118.  
  119.         clear(&FontName);
  120.         strncpy(FontName,(STRPTR)Opts[OPT_NAME],NAME_SIZE-EXT_SIZE-1);
  121.         unless(ReplaceFont=OpenDiskFont(&ReplaceAttr))
  122.         {
  123.             if(strlen(FontName)>BASE_SIZE)    /* Name too long */
  124.                 goto NoReplaceFont;
  125.             strcat(FontName,FONT_EXT);            /* Now try concatenating ".font" */
  126.             unless(ReplaceFont=OpenDiskFont(&ReplaceAttr))
  127.                 goto NoReplaceFont;
  128.         }
  129.  
  130.         FontError=ERROR_OBJECT_WRONG_TYPE;    /* Possible error: wrong type of font */    
  131.         if(Match(ReplaceFont,StdFont))            /* Fonts match themselves */
  132.         {
  133.             Forbid();
  134.             CopyAttrs(ReplaceFont,StdFont);
  135.             Permit();
  136.             unless(Opts[OPT_QUIET])
  137.                 VPrintf(INFO_TEMPLATE MSG_REPLACED,&StdAttr);
  138.             continue;
  139.         }
  140.         CloseFont(ReplaceFont);
  141. NoReplaceFont:
  142.         CloseFont(StdFont);
  143. NoStdFont:
  144.         VPrintf(INFO_TEMPLATE,&StdAttr);
  145.         PrintFault(FontError,MSG_NOT_REPLACED);
  146.         SetIoErr(FontError);
  147.         ErrOccured=TRUE;
  148.     }
  149.     RC=ErrOccured ? RETURN_ERROR : RETURN_OK;
  150.  
  151.     FreeArgs(Args);
  152. NoArgs:
  153.     CloseLibrary(DiskfontBase);
  154. InvalidDiskfont:
  155.     CloseLibrary((struct Library *)GfxBase);
  156. InvalidGfx:
  157.     CloseLibrary((struct Library *)DOSBase);
  158. InvalidDOS:
  159.     return(RC);
  160. }
  161.  
  162. STATIC BOOL Match(struct TextFont *srcFont, struct TextFont *destFont)
  163. {
  164.     return((BOOL)(srcFont->tf_YSize==destFont->tf_YSize &&
  165.                                 srcFont->tf_Style==destFont->tf_Style &&
  166.                                 (srcFont->tf_Flags & FLAGS_MASK)==
  167.                                     (destFont->tf_Flags & FLAGS_MASK) &&
  168.                                 srcFont->tf_XSize==destFont->tf_XSize &&
  169.                                 srcFont->tf_Baseline==destFont->tf_Baseline));
  170. }
  171.  
  172. STATIC VOID CopyAttrs(struct TextFont *srcFont, struct TextFont *destFont)
  173. {
  174.     destFont->tf_BoldSmear=srcFont->tf_BoldSmear;
  175.     destFont->tf_Modulo=srcFont->tf_Modulo;
  176.     destFont->tf_LoChar=srcFont->tf_LoChar;
  177.     destFont->tf_HiChar=srcFont->tf_HiChar;
  178.     destFont->tf_CharData=srcFont->tf_CharData;
  179.     destFont->tf_CharLoc=srcFont->tf_CharLoc;
  180.     destFont->tf_CharSpace=srcFont->tf_CharSpace;
  181.     destFont->tf_CharKern=srcFont->tf_CharKern;
  182. }
  183.